﻿import { createRouter, createWebHistory } from "vue-router";
import Login from '@/component/Login copy.vue';
import About from '@/component/About.vue';
import Customize from '@/component/Customize.vue';
import My_templates from '@/component/My-templates.vue';
import Ide_templates from '@/component/Ide-templates.vue';


const router = createRouter({
    history: createWebHistory(), // 改为历史模式
    routes: [
        {
            path: '/',
            component: About
        },
        {
            path: '/customize',
            component: Customize
        },
        {
            path: '/my-templates',
            component: My_templates
        },
        {
            path: '/ide-templates',
            component: Ide_templates
        },
        {
            path: '/login',
            component: Login
        }
    ]
});

router.beforeEach((to, from, next) => {
    const isAuthenticated = !!localStorage.getItem('authToken');
  
    if (!isAuthenticated && to.path !== '/login') {
        next('/login');
    } else if (isAuthenticated && to.path === '/login') {
        next('/');
    } else {
        next();
    }
});

export default router;
